<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <label class="el-form-item-label">{{ $t('taskResult.taskId') }}</label>
        <el-input v-model="query.wmsTaskId" clearable :placeholder="$t('taskResult.taskId')" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">{{ $t('taskResult.taskType') }}</label>
        <el-select v-model="query.wcsTaskType" class="filter-item" style="width: 185px;" clearable filterable :placeholder="$t('common.selectPlaceholder')" @change="crud.toQuery">
          <el-option
            v-for="item in dict.task_result_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">{{ $t('taskResult.taskStatus') }}</label>
        <el-select v-model="query.status" class="filter-item" style="width: 185px;" clearable filterable :placeholder="$t('common.selectPlaceholder')" @change="crud.toQuery">
          <el-option
            v-for="item in dict.task_result_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column prop="wmsTaskId" :label="$t('taskResult.taskId')" />
        <el-table-column prop="fromLocation" :label="$t('taskResult.realFrom')" />
        <el-table-column prop="toLocation" :label="$t('taskResult.realTo')" />
        <el-table-column prop="created" :label="$t('taskResult.realComplete')" />
        <el-table-column prop="status" :label="$t('taskResult.taskStatus')">
          <template slot-scope="scope">
            {{ dict.label.task_result_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="issueCode" :label="$t('taskResult.errorCode')" />
        <el-table-column prop="message" :label="$t('taskResult.errorMsg')" />
        <el-table-column prop="wcsTaskType" :label="$t('taskResult.taskType')">
          <template slot-scope="scope">
            {{ dict.label.task_result_type[scope.row.wcsTaskType] }}
          </template>
        </el-table-column>
        <el-table-column prop="lpnCode" :label="$t('taskResult.containerCode')" />
        <el-table-column prop="lastUpdated" :label="$t('taskResult.lastTime')" />
        <el-table-column v-if="user.username === 'inx'" :label="$t('common.operation')" width="150px" align="center">
          <template slot-scope="scope">
            <!-- <udOperation
              :data="scope.row"
              :permission="permission"
            /> -->
            <el-popconfirm
              :confirm-button-text="$t('crud.confirm')"
              :cancel-button-text="$t('crud.cancel')"
              :title="$t('taskResult.msg1')"
              @confirm="forceComplete(scope.row, scope.$index)"
            >
              <el-button
                slot="reference"
                type="danger"
                :loading="btn.force === scope.$index"
                icon="el-icon-cpu"
                size="mini"
              >{{ $t('taskResult.forceComplete') }}</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudTaskResult from '@/api/taskResult'
import { forceCompleteAPI } from '@/api/taskResult'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
// import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, wmsTaskId: null, fromLocation: null, toLocation: null, created: null, status: null, issueCode: null, message: null, wcsTaskType: null, lpnCode: null, lastUpdated: null }
export default {
  name: 'TaskResult',
  components: { pagination, crudOperation, rrOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['task_result_status', 'task_result_type'],
  cruds() {
    return CRUD({ title: '立库运行中任务', url: 'api/taskResult', idField: 'id', sort: 'id,desc', crudMethod: { ...crudTaskResult },
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: true,
        reset: true
      }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'taskResult:add'],
        edit: ['admin', 'taskResult:edit'],
        del: ['admin', 'taskResult:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'wmsTaskId', display_name: '任务id' },
        { key: 'wcsTaskType', display_name: '任务类型' }
      ],
      btn: {
        force: ''
      }
    }
  },
  computed: {
    user() {
      // 直接通过 this.$store.getters 访问
      return this.$store.getters.user
    }
  },
  mounted() {
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    async forceComplete(row, index) {
      try {
        this.btn.force = index
        const res = await forceCompleteAPI(row.wmsTaskId)
        if (res.meta.code === 200) {
          this.$message.success(this.$t('taskResult.msg2'))
          this.crud.toQuery()
        } else {
          this.$message.warning(res.data.mesMessage.errorMessage)
        }
      } finally {
        this.btn.force = false
      }
    }
  }
}
</script>

<style scoped>

</style>
